package com.bwf.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/6/6 16:02
 * @Copyright 博为峰
 */
public class ServerDemo01 implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init");
        String name = servletConfig.getInitParameter("name");
        System.out.println("name = " + name);
        ServletContext servletContext = servletConfig.getServletContext();

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //-------------------------请求----------------------------------------------------
        //主机
        String serverName = servletRequest.getServerName();
        System.out.println("serverName = " + serverName);
        //协议
        String protocol = servletRequest.getProtocol();
        System.out.println("protocol = " + protocol);
        //端口
        int serverPort = servletRequest.getServerPort();
        System.out.println("serverPort = " + serverPort);
        //获取请求参数
        String name = servletRequest.getParameter("name");
        System.out.println("name = " + name);
        //-------------------------响应----------------------------------------------------
        PrintWriter printWriter = servletResponse.getWriter();
        printWriter.write("<a href='https://www.atstudy.com/'>atstudy</a>");
        System.out.println("service");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("destroy");
    }
}
